/*
 * PurchaseOrderBO.java
 *
 * Created on February 28, 2007, 8:58 AM
 *
 */

package edu.byu.isys413.group1E.data;

import edu.byu.isys413.group1E.*;
import edu.byu.isys413.group1E.data.UpdateController;
import java.sql.SQLException;
import java.util.ArrayList;

/** Contains information about a PurchaseOrder, storing the date the PurchaseOrder
 * occurred, the tax rate, the sub-total, tax, and total amount for the PurchaseOrder.
 * The PurchaseOrderBO also keeps track of the PurchaseOrder lines that pertain to it,
 * the coupons that are applied to the PurchaseOrder, as well as the store, employee,
 * customer, and ID of the payment made.
 *
 * If the PurchaseOrder is a return PurchaseOrder, there is a variable to store the
 * ID of the original PurchaseOrder from which the return is made.
 *
 * @author Travis Kulbeth
 */
public class PurchaseOrderBO {
    
    private String POID;
    private double POTotal = 0;
    private ArrayList<OrderLineBO> OrderLineList = new ArrayList<OrderLineBO>();
    private VendorBO vendor;
    boolean alreadyInDB;
    boolean isDirty;
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
    /** Creates a new instance of PurchaseOrderBO */
    PurchaseOrderBO(){
    }
    
    /** Creates a new instance of PurchaseOrderBO with an ID*
     *
     * @param transID contains the already existing ID for the PurchaseOrder to be created.
     */
    PurchaseOrderBO(String POID) {//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        setPOID(POID);
        setIsDirty(true);
        }
               
    /** Calculates the total for this PurchaseOrder, including tax and discounts*/
    public void calcTotal(){
        for(int counter = 0; counter < getOrderLineList().size(); counter++){
            POTotal = POTotal + getOrderLineList().get(counter).getLineAmount();
        }                
    }
        
    /** Sets the alreadyInDB variable to indicate that
     * the information contained in this object has been stored in the database
     *
     * @param is contains the alreadyInDB status
     */
    public void setAlreadyInDB(boolean is){
        setIsDirty(true);
        this.alreadyInDB = is;
    }
    
    /** Returns if the information contained in this object has already been stored in the database
     * @return if the object information already exists in the DB
     */
    public boolean isAlreadyInDB() {
        return alreadyInDB;
    }
    
    /** Returns if information has been changed within this object
     * @return if the DB needs to be updated with new info.
     */
    public boolean isIsDirty() {
        return isDirty;
    }
    
    /** Sets the variable isDirty to indicate that some information has been modified or changed
     *in this object
     *
     * @param isDirty indicates whether information has changed
     */
    public void setIsDirty(boolean isDirty) {
        this.isDirty = isDirty;
    }
    
    /** Returns the PurchaseOrder ID, which was initially generated by the GUID class
     * @return the transID
     */
    public String getPOID() {
        return POID;
    }
    
    /** Sets the PurchaseOrder ID if needed
     * @param transID contains the PurchaseOrderID to be used for this obejct
     */
    public void setPOID(String POID) {
        setIsDirty(true);
        this.POID = POID;
    }
    
    /** Returns the PurchaseOrder total calculated in this PurchaseOrder
     * @return transTotal
     */
    public double getPOTotal() {
        return POTotal;
    }
    
    /** Sets the PurchaseOrder total calculated in this PurchaseOrder
     * @param transTotal contains the total for this PurchaseOrder
     */
    public void setPOTotal(double POTotal) {
        setIsDirty(true);
        this.POTotal = POTotal;
    }
            
    /** Returns the list of OrderLineBOs (PurchaseOrder lines) pertaining to
     *this PurchaseOrder.
     *
     *@return List of all PurchaseOrder lines for this PurchaseOrder
     */
    public ArrayList<OrderLineBO> getOrderLineList(){
        if(OrderLineList.isEmpty()){
            try {
                this.setOrderLineList(OrderLineDAO.getInstance().readByPO(this.getPOID()));
            } catch (ConnectionPoolException ex) {
                ex.printStackTrace();
            } catch (DataException ex) {
                ex.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return this.OrderLineList;
    }
    
    /** Adds an OrderLineBO to the Order Line list to be used in calculating
     *discounts.
     *
     *@param OrderLine contains a new PurchaseOrder line object for this PurchaseOrder
     */
    public void addOrderLine(OrderLineBO orderLine){
        setIsDirty(true);
        orderLine.calcLineAmount();
        this.getOrderLineList().add(orderLine);
    }

    /**
     * 
     * @return 
     */
    public VendorBO getVendor() {
        return vendor;
    }

    /**
     * 
     * @param vendor 
     */
    public void setVendor(VendorBO vendor) {
        setIsDirty(true);
        this.vendor = vendor;
    }

    /**
     * 
     * @param OrderLineList 
     */
    public void setOrderLineList(ArrayList<OrderLineBO> OrderLineList) {
        this.OrderLineList = OrderLineList;
    }
    
}//end PurchaseOrderBO class